package com.xzz.finger;

/**
 * @author: hhz
 * @create: 2022-02-17 13:54
 * 剑指 Offer 13. 机器人的运动范围
 **/
public class MovingCount {
    public static void main(String[] args) {
        System.out.println(movingCount(2, 3, 1));
        System.out.println(movingCount(3, 1, 0));
    }

    static int res = 0;

    public static int movingCount(int m, int n, int k) {
        res = 0;
        int[][] arr = new int[m][n];
        dfs(arr, 0, 0, k);
        return res;
    }

    public static void dfs(int[][] arr, int i, int j, int k) {
        if (i < 0 || i >= arr.length || j < 0 || j >= arr[0].length) {
            return;
        }
        if (arr[i][j] != 0) {
            return;
        }
        if (!judge(i, j, k)) {
            return;
        }
        arr[i][j] = 1;
        res++;
        dfs(arr, i + 1, j, k);
        dfs(arr, i - 1, j, k);
        dfs(arr, i, j + 1, k);
        dfs(arr, i, j - 1, k);

    }

    public static boolean judge(int i, int j, int k) {
        int a = i / 10;
        int b = i % 10;
        int c = j / 10;
        int d = j % 10;
        if ((a + b + c + d) > k) {
            return false;
        } else {
            return true;
        }
    }
}
